@echo off
chcp 65001 >nul

:: 检查Maven是否已安装
where mvn >nul 2>&1
if %errorlevel% equ 0 (
    echo Maven已安装
    mvn --version
    goto :end
)

echo Maven未安装，开始安装...

:: 设置Maven版本变量
set MAVEN_VERSION=3.9.6
set MAVEN_URL=https://dlcdn.apache.org/maven/maven-3/%MAVEN_VERSION%/binaries/apache-maven-%MAVEN_VERSION%-bin.zip

:: 检查maven.zip是否已存在
if exist maven.zip (
    echo Maven安装包已存在，跳过下载...
) else (
    :: 下载Maven
    echo 正在下载Maven %MAVEN_VERSION%...
    powershell -Command "Invoke-WebRequest -Uri '%MAVEN_URL%' -OutFile 'maven.zip'"
    if %errorlevel% neq 0 (
        echo 下载失败
        exit /b 1
    )
)

:: 解压Maven
echo 正在解压Maven...
powershell -Command "Expand-Archive -Path 'maven.zip' -DestinationPath ."
if %errorlevel% neq 0 (
    echo 解压失败
    exit /b 1
)

del maven.zip

:: 设置环境变量
setx MAVEN_HOME "%cd%\apache-maven-%MAVEN_VERSION%"
setx PATH "%%PATH%%;%%MAVEN_HOME%%\bin"

:: 验证安装
call "%MAVEN_HOME%\bin\mvn.cmd" --version
if %errorlevel% neq 0 (
    echo 安装验证失败
    exit /b 1
)

echo Maven %MAVEN_VERSION% 安装成功

:end
pause